<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RSA密钥生成器</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        select, button {
            padding: 10px;
            margin-bottom: 20px;
            border-radius: 4px;
            border: 1px solid #ddd;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            font-weight: bold;
            width: 100%;
        }
        button:hover {
            background-color: #45a049;
        }
        .download-buttons {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        .download-buttons button {
            flex: 1;
        }
        .key-display {
            background-color: #f8f8f8;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 10px;
            white-space: pre-wrap;
            overflow-x: auto;
            font-family: monospace;
            margin-bottom: 20px;
            max-height: 200px;
            overflow-y: auto;
        }
        .download-button {
            background-color: #2196F3;
        }
        .download-button:hover {
            background-color: #0b7dda;
        }
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>RSA密钥生成器</h1>
        
        <div>
            <label for="key-size">密钥长度:</label>
            <select id="key-size">
                <option value="1024">1024 位</option>
                <option value="2048" selected>2048 位 (推荐)</option>
                <option value="4096">4096 位</option>
            </select>
            <p style="color: #666; font-size: 0.9em; margin-top: 5px;">推荐使用默认的2048位密钥长度，安全性和性能的最佳平衡。</p>
        </div>
        
        <button id="generate-button">生成RSA密钥对</button>
        
        <div id="key-section" class="hidden">
            <h2>公钥</h2>
            <div id="public-key" class="key-display"></div>
            
            <h2>私钥</h2>
            <div id="private-key" class="key-display"></div>
            
            <div class="download-buttons">
                <button id="download-public" class="download-button">下载公钥</button>
                <button id="download-private" class="download-button">下载私钥</button>
            </div>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const generateButton = document.getElementById('generate-button');
            const keySection = document.getElementById('key-section');
            const publicKeyDisplay = document.getElementById('public-key');
            const privateKeyDisplay = document.getElementById('private-key');
            const downloadPublicButton = document.getElementById('download-public');
            const downloadPrivateButton = document.getElementById('download-private');
            
            generateButton.addEventListener('click', async function() {
                const keySize = document.getElementById('key-size').value;
                generateButton.textContent = '生成中...';
                generateButton.disabled = true;
                
                try {
                    const response = await fetch(`/generate?key_size=${keySize}`, {
                        method: 'POST'
                    });
                    
                    if (!response.ok) {
                        throw new Error('生成密钥失败');
                    }
                    
                    const data = await response.json();
                    publicKeyDisplay.textContent = data.public_key;
                    privateKeyDisplay.textContent = data.private_key;
                    keySection.classList.remove('hidden');
                } catch (error) {
                    alert('生成密钥时出错: ' + error.message);
                } finally {
                    generateButton.textContent = '生成RSA密钥对';
                    generateButton.disabled = false;
                }
            });
            
            downloadPublicButton.addEventListener('click', function() {
                window.location.href = '/download/public';
            });
            
            downloadPrivateButton.addEventListener('click', function() {
                window.location.href = '/download/private';
            });
        });
    </script>
</body>
</html>